//
//  FindPictureCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/4/8.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "FindPictureCollectionViewCell.h"
#import "FindPictureModel.h"
#import "FindPictureContentCollectionViewCell.h"
#import "FindPictureCollectionReusableView.h"
#import "FIndPictureSecondModel.h"



@interface FindPictureCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)NSMutableArray *dataArr;

@property (nonatomic, retain)NSArray *sectionArr;

@property (nonatomic, retain)UICollectionView *collectionV;

@end

@implementation FindPictureCollectionViewCell

-(void)dealloc
{
    [_findPicId release];
    [_dataArr release];
    [_sectionArr release];
    [super dealloc];
    
}

//- (NSMutableArray *)dataArr{
//    if (!_dataArr) {
//        self.dataArr = [NSMutableArray array];
//    }
//    return [[_dataArr retain] autorelease];
//}


#pragma mark - collectionView的初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionV];
    }
    return self;
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
}


#pragma mark - 数据解析在这里实现
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self dataHandle];
}



#pragma mark - 创建collectionView
- (void)createCollectionV
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((BOUNDS_WIDTH - 20 - 2 *10) / 3, 80);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(BOUNDS_WIDTH, 40);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.contentView addSubview:self.collectionV];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    
    [self.collectionV registerClass:[FindPictureContentCollectionViewCell class] forCellWithReuseIdentifier:@"FindPictureContentCollectionViewCell"];
    
    [self.collectionV registerClass:[FindPictureCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FindPictureCollectionReusableView"];
    
    [layout release];
    [_collectionV release];
    
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    FindPictureModel *FPModel = self.dataArr[section];
    return FPModel.photosArr.count;
    
}

#pragma mark - cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindPictureContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindPictureContentCollectionViewCell" forIndexPath:indexPath];
    
    
    FindPictureModel *fModel = self.dataArr[indexPath.section];
    FIndPictureSecondModel *fpsModel = fModel.photosArr[indexPath.item];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:fpsModel.smallPath]];
    
    return cell;
}

#pragma mark - 设置collectionView的section的数量
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArr.count;
}

#pragma mark - 设置collectionView的section的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FindPictureCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FindPictureCollectionReusableView" forIndexPath:indexPath];
    FindPictureModel *pModel = self.dataArr[indexPath.section];
    reusableView.titleLab.text = pModel.title;
   
    reusableView.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:235 / 255.0 blue:235 / 255.0 alpha:1];
    //reusableView.titleLab.text =  self.sectionArr[indexPath.section][@"title"];
    
   // reusableView.backgroundColor = [UIColor redColor];
    return reusableView;
}



#pragma mark - 数据解析
- (void)dataHandle
{
    //self.dataArr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/price/nav_pictures?serialGroupId=%@&modelId=nul", self.findPicId];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
       
        self.sectionArr = responseObject[@"sections"];
        self.dataArr = [FindPictureModel transformWithArray:self.sectionArr];
        
        [_collectionV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}




@end
